package action.paiement;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import bdd.admin.UpdateEtatCommande;
import bean.client.Client;
import bean.produit.Panier;

import service.generationpdf.GestionGenerationPDF;

import action.general.NotreServlet;


public class PaiementAction extends NotreServlet{

	private static Log logger = LogFactory.getLog(action.paiement.LogicWebEtapesAction.class);

	public ActionForward execute( ActionMapping pMapping, ActionForm pForm, HttpServletRequest pRequest, HttpServletResponse pResponse)
	{
	
		ActionErrors errors = new ActionErrors();
		int id_commande;
		double montant;
		int prix;
		
        Calendar calendar = Calendar.getInstance();
        StringBuffer stringbuffer;
        ServletOutputStream servletoutputstream;
        HttpSession httpsession = pRequest.getSession();
        
        id_commande= Integer.parseInt((String)pRequest.getAttribute("Num_Commdande"));
        montant=Double.parseDouble((String)pRequest.getAttribute("Montant"));
        
        prix=(int)montant;
        
       
        
      
        
        
        //BeanEtudiant beanetudiant = (BeanEtudiant)httpsession.getAttribute("beanetudiant");
        byte abyte0[];
        int i;
        
        try
        {
        	logger.debug("appel LogicWebPaiementExt\351rieur");
                	
        	stringbuffer = LogicWebPaiementExterieur.paiementExterieur(pRequest,id_commande,prix);
            
            if(stringbuffer == null)
            {
            	logger.debug("dans if is == null");
                return pMapping.findForward("redirectionAdresseMail");
            }
        }
        catch(Exception exception)
        {
            /*httpsession = pRequest.getSession(false);
            String s = (String)httpsession.getAttribute("ref_etu");
            if(s != null)
            {
            	logger.error((new StringBuilder()).append("Erreur lors de l'appel au paiement ext\351rieur pour l'etudiant : ").append(s).append("#").append(calendar.getTime()).append(", ").toString(), exception);
            } else
            {
            	logger.error("Pas de ref_etu en session, ", exception);
                s = Long.toString(System.currentTimeMillis());
            }*/
            return pMapping.findForward("error");
        }
        
        
        
        try {
			servletoutputstream = pResponse.getOutputStream();
			i = 0;
	        i = stringbuffer.toString().length();
	        abyte0 = stringbuffer.toString().getBytes();
	        servletoutputstream.write(abyte0, 0, i);
	        
	        UpdateEtatCommande.SetEtatCommande(id_commande);
	        
	        
	        Object objClient = httpsession.getAttribute("client");
			Client client = (Client)objClient;
			

	        Object objPanier = httpsession.getAttribute("panier");
			Panier panier = (Panier)objPanier;
			       
			
	        GestionGenerationPDF.genereFichierPdf(Integer.toString(client.getId()), Integer.toString(id_commande), pRequest.getSession().getServletContext(), panier, panier.getMontantTotal()-panier.getTotalAPayer());

	        httpsession.removeAttribute("client");
	        httpsession.removeAttribute("panier");
	        
	        return null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			errors.add("errors",new ActionMessage("errors.exception"));
			saveErrors(pRequest,errors); 
			return (new ActionForward(pMapping.getInput()));
		}
		} 
        

    private static final String NOM_CLASSE_COMPLET = LogicWebEtapesAction.class.getName();
    private static final String NCC = NOM_CLASSE_COMPLET;
    private static final String NOM_CLASSE = NOM_CLASSE_COMPLET.substring(NOM_CLASSE_COMPLET.lastIndexOf('.') + 1);

	
	
	
	
}
